var tableIns;//表格实例
layui.config({
    base: '../../../eui/plugins/layui-extend/' //这是你存放拓展模块的根目录
});
layui.use(['table','form','laydate'], function () {
    var table = layui.table;
    var form = layui.form;
	io.send("/mm/datasources?pageNo=1&pageSize=-1","GET",null,function(res){
		var data=res.data.list;
		var map=new Map();
		for(var i in data){
			var d=data[i];
    		map.set(d.dsId.toString(),d.dsId);
		}
		map2Select(map,'datasourceId');
	    form.render();
	},false,false,false,false);
	io.send("/mm/datastreams?pageNo=1&pageSize=-1","GET",null,function(res){
		var data=res.data.list;
		var words=new Map();
		for(var i in data){
			var d=data[i];
    		words.set(d.keyColumn,d.keyColumn);
		}
		map2Select(words,'keyword');
	    form.render();
	},false,false,false,false);
    form.render();
    tableIns = table.render({
          elem: '#lTable'
        , url: appendCtx('/mm/variables')
        , cols: [[ //表头
            {field: 'id', title: 'ID',align:'center',width:80}
            ,{field: 'name', title: '指标名',align:'center',width:200}
            ,{field: 'priority', title: '优先级',align:'center',width:80}
  			, {field: '', title: '数据来源',align:'center',width:120, templet: 
                   function(d){
				 return translate("statDataSource",d.type);  //数据字典翻译，具体看base.js中translate方法
	      	}}
            ,{field: 'dsName', title: '数据流',align:'center',width:120}
            ,{field: 'keyword', title: '关键字',align:'center',width:120}
            ,{field: 'statField', title: '统计字段',align:'center',width:180}
            ,{field: 'expression', title: '表达式',align:'center',width:200}
			, {field: '', title: '统计函数',align:'center',width:120, templet: 
                   function(d){
				 return translate("statFunc",d.statFunc);  //数据字典翻译，具体看base.js中translate方法
	      	}}
  			, {field: '', title: '数据窗口类型',align:'center',width:120, templet: 
                   function(d){
				 return translate("dataWindowType",d.dataWindowType);  //数据字典翻译，具体看base.js中translate方法
	      	}}
            ,{field: 'dataWindowSize', title: '窗口大小',align:'center',width:100}
			, {field: '', title: '时间单位',align:'center',width:100, templet: 
                   function(d){
				 return translate("timeUnit",d.timeWindowUnit);  //数据字典翻译，具体看base.js中translate方法
	      	}}
			, {field: '', title: '优先级策略',align:'center',width:120, templet: 
                   function(d){
				 return translate("priorityStrategy",d.priorityStrategy);  //数据字典翻译，具体看base.js中translate方法
	      	}}
			, {field: '', title: '输出类型',align:'center',width:120, templet: 
                   function(d){
				 return translate("outputType",d.outputType);  //数据字典翻译，具体看base.js中translate方法
	      	}}
            ,{field: 'createUserName', title: '创建者',align:'center',width:120}
            ,{field: 'createTime', title: '创建时间',align:'center',width:160}
            , {field: 'operate', title: '操作',width:200, fixed: 'right',templet: '#operateTp'}
        ]]
        , page: true
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        }
    });

    form.on('submit(search)', function(formData){
    	tableIns.reload({
            where:formData.field,
			page:{
                curr: 1
            }
        });
        return false;
      });
    
});

function refreshTableData() {
    tableIns.reload({});
}

function edit(id) {
    layer.open({
        type: 2,
        title: typeof(id) != "undefined" ? '修改' : '新增',
        shadeClose: true,
        area: ['800px', '600px'],
        content: 'edit.html?id=' + id,
    });
}

function del(id){
	io.delete("/mm/variables/"+id,function(res){
		refreshTableData();
	});
}